package com.gskj.eduservice.controller;


import com.gskj.commonutils.ResultJson;
import com.gskj.eduservice.client.VodClient;
import com.gskj.eduservice.entity.EduVideo;
import com.gskj.eduservice.service.IEduVideoService;
import com.gskj.servicebase.exceptionhandler.GuLiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-16
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    // 注入远程调用接口
    @Autowired
    private VodClient vodClient;


    @Autowired
    private IEduVideoService iEduVideoService;

    /***
     *  添加 小结
     * @param eduVideo
     * @return
     */
    @PostMapping("/add/video")
    public ResultJson addVideo(
            @RequestBody EduVideo eduVideo) {
        boolean save = iEduVideoService.save(eduVideo);
        return save == true ? ResultJson.success() : ResultJson.error();
    }

    // 修改小结
    @PostMapping("/edit/video")
    public ResultJson eidtVideo(
            @RequestBody EduVideo eduVideo) {
        boolean b = iEduVideoService.updateById(eduVideo);
        return b == true ? ResultJson.success() : ResultJson.error();
    }


    /***
     *  删除 小结和小结对应的视频
     * @param id
     * @return
     */
    @GetMapping("/remove/video/{id}")
    public ResultJson eidtVideo(
            @PathVariable("id") String id) {

        // 查询小结 ，获取小结中对应的视频id
        EduVideo video = iEduVideoService.getById(id);

        // 视频id
        String videoSourceId = video.getVideoSourceId();
        // 非空判断
        if(!StringUtils.isEmpty(videoSourceId)){

            // 调用远程方法 根据获取的视频id 删除视频
            ResultJson json = vodClient.removeAliyunVideo(videoSourceId);
            if(!json.getSuccess()){
                throw new GuLiException(20001,"调用远程接口失败！ 熔断执行");
            }
        }

        // 删除小结
        boolean b = iEduVideoService.removeById(id);

        return b == true ? ResultJson.success() : ResultJson.error();
    }

    /***
     *  根据id 查询  小结 信息
     * @return
     */
    @GetMapping("/find/video/{id}")
    public ResultJson findVideoById(
            @PathVariable("id") String id) {
        EduVideo video = iEduVideoService.getById(id);
        return ResultJson.success().data("videoInfo", video);
    }
}

